﻿using System;
using System.Runtime.InteropServices;
using System.ComponentModel;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Reflection;
using System.IO.Ports;
using System.Windows.Forms;


namespace csharpAx
{
    /// <summary>
    /// A very simple interface to test ActiveX with.
    /// </summary>
    [
        Guid("E86A9038-368D-4e8f-B389-FDEF38935B2F"),
        InterfaceType(ComInterfaceType.InterfaceIsDual),
        ComVisible(true)
    ]
    public interface ICashDrawer
    {
        [DispId(1)]
        string Hello();

        [DispId(2)]
        int ShowDialog(string msg);
        [DispId(3)]
        void OpenCashDrawer();
    };

    [
        Guid("873355E1-2D0D-476f-9BEF-C7E645024C32"),

        // This is basically the programmer friendly name
        // for the guid above. We define this because it will
        // be used to instantiate this class. I think this can be
        // whatever you want. Generally it is
        // [assemblyname].[classname]
        ProgId("csharpAx.CashDrawerManager"),

        // No class interface is generated for this class and
        // no interface is marked as the default.
        // Users are expected to expose functionality through
        // interfaces that will be explicitly exposed by the object
        // This means the object can only expose interfaces we define
        ClassInterface(ClassInterfaceType.None),

        // Set the default COM interface that will be used for
        // Automation. Languages like: C#, C++ and VB
        // allow to query for interface's we're interested in
        // but Automation only aware languages like javascript do
        // not allow to query interface(s) and create only the
        // default one
        ComDefaultInterface(typeof(ICashDrawer)),
        ComVisible(true)
    ]
    public class CashDrawerManager : ICashDrawer
    {
     
        #region [IHello implementation]
        SerialPort serialPort;



        public string Hello()
        {
            return "Hello from CHello object";
        }


        public int ShowDialog(string msg)
        {
            System.Windows.Forms.MessageBox.Show(msg, "Test1");
            return 0;
        }

        public void OpenCashDrawer()
        {
            try
            {
                serialPort = new SerialPort();
                serialPort.PortName = "COM1"; 
                if (serialPort.IsOpen == false)
                    serialPort.Open();
                const int A = (int)'A';
                const int Z = (int)'Z';
                for (int i = A; i <= Z; i++)
                {
                    serialPort.Write(Convert.ToChar(i).ToString());
                }

                serialPort.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(" " + ex.Message + " !!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
 
        }

        #endregion
    };
}
